package com.smart.system.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.smart.common.constant.GlobalConstants;
import com.smart.common.core.domain.entity.SysDept;
import com.smart.common.utils.DateUtils;
import com.smart.system.mapper.SysDeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smart.system.mapper.WorkingScheduleMapper;
import com.smart.system.domain.WorkingSchedule;
import com.smart.system.service.IWorkingScheduleService;

/**
 * 排班Service业务层处理
 * 
 * @author smart
 * @date 2024-09-18
 */
@Service
public class WorkingScheduleServiceImpl extends ServiceImpl<WorkingScheduleMapper,WorkingSchedule> implements IWorkingScheduleService {

    @Autowired
    private SysDeptMapper sysDeptMapper;

    /**
     * 查询排班
     * 
     * @param id 排班主键
     * @return 排班
     */
    @Override
    public WorkingSchedule selectWorkingScheduleById(String id)
    {
        return this.baseMapper.selectById(id);
    }

    /**
     * 查询排班列表
     * 
     * @param workingSchedule 排班
     * @return 排班
     */
    @Override
    public List<WorkingSchedule> selectWorkingScheduleList(WorkingSchedule workingSchedule)
    {
        return this.list(Wrappers.lambdaQuery(workingSchedule));
    }

    /**
     * 新增排班
     * 
     * @param workingSchedule 排班
     * @return 结果
     */
    @Override
    public int insertWorkingSchedule(WorkingSchedule workingSchedule)
    {
        workingSchedule.setCreateTime(DateUtils.getNowDate());
        return this.baseMapper.insert(workingSchedule);
    }

    /**
     * 修改排班
     * 
     * @param workingSchedule 排班
     * @return 结果
     */
    @Override
    public int updateWorkingSchedule(WorkingSchedule workingSchedule)
    {
        workingSchedule.setUpdateTime(DateUtils.getNowDate());
        return this.baseMapper.updateById(workingSchedule);
    }

    /**
     * 批量删除排班
     * 
     * @param ids 需要删除的排班主键
     * @return 结果
     */
    @Override
    public int deleteWorkingScheduleByIds(String[] ids)
    {
        return this.baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除排班信息
     * 
     * @param id 排班主键
     * @return 结果
     */
    @Override
    public int deleteWorkingScheduleById(String id)
    {
        return this.baseMapper.deleteById(id);
    }

    @Override
    /**
     * 递归向上获取部门排班,没有则返回null
     * @param deptId 部门id
     */
    public WorkingSchedule getWorkingSchedule(Long deptId) {
        //获取部门排班
        WorkingSchedule one = this.getOne(Wrappers.lambdaQuery(WorkingSchedule.class).eq(WorkingSchedule::getDeptId, deptId));
        //如果部门排班存在,则直接返回
        if (one != null) {
            return one;
        }
        //获取部门信息
        SysDept sysDept = sysDeptMapper.selectDeptById(deptId);
        if(sysDept == null){
            return null;
        }
        if(!GlobalConstants.PARENT_ID.equals(sysDept.getParentId())){
            return getWorkingSchedule(sysDept.getParentId());
        }else{
            return null;
        }
    }
}
